package com.mt.fpb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.mt.fpb.common.util.DateUtil;
import com.mt.fpb.mapper.TMineInfoMapper;
import com.mt.fpb.mapper.TWarningMapper;
import com.mt.fpb.model.TWarning;
import com.mt.fpb.model.dto.BaseQueryParams;
import com.mt.fpb.model.vo.CommonResult;
import io.swagger.models.auth.In;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

import static com.mt.fpb.common.util.DateUtil.getPastDate;

/**
 * 矿山越界开采预警系统接口 LT
 * <p>
 * 2021年6月13日09:42:22
 */
@RestController
@RequestMapping("/OverstepBoundary")
public class OverstepBoundaryController {

    @Resource
    private TWarningMapper tWarningMapper;
    @Resource
    private TMineInfoMapper tMineInfoMapper;

    /**
     * 累计数据统计 LT
     * 2021年6月13日09:43:37
     * 左上
     *
     * @param queryParams
     * @return
     */
    @GetMapping("accumulatedData")
    public CommonResult accumulatedData(BaseQueryParams queryParams) {
        Example example = new Example(TWarning.class);
        JSONObject json = new JSONObject();
        //运营矿产总计
        json.put("mineCount", tMineInfoMapper.selectAll().size());
        //越界预警总计
        example.createCriteria().orEqualTo("warningType", 6).orEqualTo("mineId", queryParams.getMineId());
        json.put("overstepBoundary", tWarningMapper.selectCountByExample(example));
        example.clear();
        //消除越界风险
        example.createCriteria().andIsNotNull("dealWithTime").andEqualTo("mineId", queryParams.getMineId());
        json.put("eliminate", tWarningMapper.selectCountByExample(example));
        example.clear();
       /* //核实违法开采
        example.createCriteria().andEqualTo("warningType", 2).andIsNotNull("dealWithTime").andEqualTo("mineId", queryParams.getMineId());
        json.put("beIllegal ", tWarningMapper.selectByExample(example).size());
        //处置违法矿产
        example.createCriteria().andEqualTo("warningType", 3).andIsNotNull("dealWithTime").andEqualTo("mineId", queryParams.getMineId());
        json.put("dispose ", tWarningMapper.selectByExample(example).size());*/
        return CommonResult.success(json);
    }

    /**
     * 越界预警 LT
     * 左下
     * 2021年6月13日10:02:06
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getEarlyWarning")
    public CommonResult getEarlyWarning(BaseQueryParams queryParams) {
        Example example = new Example(TWarning.class);
        //越界预警记录
        example.createCriteria().andEqualTo("warningType", 6).andEqualTo("mineId", queryParams.getMineId());
        example.orderBy("updateTime").desc();
        //查询前20跳记录
        PageHelper.startPage(1, 20);
        List<TWarning> tWarnings = tWarningMapper.selectByExample(example);
        return CommonResult.success(tWarnings);
    }

    /**
     * 今年合规开采率
     * 右上
     * 2021年6月13日10:08:11
     *
     * @param queryParams
     * @return
     */
    @GetMapping("compliance")
    public CommonResult compliance(BaseQueryParams queryParams) {
        //合规开采数
        Integer count = tWarningMapper.compliance(DateUtil.getCurrentDate("yyyy"), queryParams.getMineId());
        //开采率
        if (StringUtils.isEmpty(count)) {
            count = 0;
        }
        int result = (1 - count) * 100;
        return CommonResult.success(result);
    }

    /**
     * 近7日预警统计
     * 中下
     * 2021年6月13日10:13:42
     *
     * @return
     */
    @GetMapping("warningStatistics")
    public CommonResult warningStatistics(BaseQueryParams queryParams) {
        Example example = new Example(TWarning.class);
        JSONObject json = new JSONObject(new LinkedHashMap<>());
        //获取最近7日时间
        List<String> list = DateUtil.findDates(DateUtil.convert2Date(getPastDate(6, "yyyy-MM-dd"), "yyyy-MM-dd"), DateUtil.convert2Date(DateUtil.getCurrentDate("yyyy-MM-DD"), "yyyy-MM-DD"));
        //越界开采集合
        List<Integer> earlyWarning = new LinkedList<>();
        //预警矿产数量
        List<Integer> statisticsList = new LinkedList<>();
        //遍历
        for (String str : list) {
            //越界开采条件
            example.createCriteria().andEqualTo("insertTime", str).andEqualTo("warningType", 6);
            //存入集合
            earlyWarning.add(tWarningMapper.selectCountByExample(example));

            statisticsList.add(tWarningMapper.getStatisticsList(str));
        }
        json.put("statisticsList", statisticsList);
        json.put("earlyWarning", earlyWarning);
        json.put("time", list);
        return CommonResult.success(json);
    }


}
